/**
 * 
 */
package com.EstateManager.Dao;



import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Admin;
import com.EstateManager.Entity.User;

/**
 * 
 * 创建时间 : 下午9:58:23
 * @author Yonggang Liu
 *
 * AdminDao.java
 */
@Repository("userDao")
public class UserDao implements IModelBasicOperate<User, Integer>{

	@Autowired
	private SessionFactory sessionFactory;
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	
	
	@Override
	public User load(Integer id) throws HibernateException{
		
		return (User)this.getCurrentSession().load(User.class, id);
	}

	@Override
	public User get(Integer id) throws HibernateException{
		
		return (User)this.getCurrentSession().get(User.class, id);
	}

	
	
	
	@Override
	public void persist(User entity) throws HibernateException{
		
		this.getCurrentSession().persist(entity);
	}

	
	
	@Override
	public Integer save(User entity) throws HibernateException{
		
		return (Integer)this.getCurrentSession().save(entity);
	}

	
	@Override
	public void saveOrUpdate(User entity) throws HibernateException{
		this.getCurrentSession().saveOrUpdate(entity);
	}

	
	@Override
	public void detete(User entity) throws HibernateException{
		this.getCurrentSession().delete(entity);
	}

	
	@Override
	public void flush() throws HibernateException{
		this.getCurrentSession().flush();
	}
	
	/**
	 * 根据用户名查询用户
	 * @param userName 用户名
	 * @return user
	 * */	
	public User getUserByUsername(String userName) {

		
		Session session = this.getCurrentSession();
		Query query = session.createQuery("select user"
				+ " from User user"
				+ " where user.username = :username");
		query.setString("username", userName);
		return (User)query.uniqueResult();
	}
		/**
		 * 根据昵称查询用户
		 * @param nickName 用户昵称
		 * @return user
		 * */	
public User getUserByNickName(String nickName) {
		Session session = this.getCurrentSession();
		Query query = session.createQuery("select user"
				+ " from User user"
				+ " where user.nickName = :nickName");
		query.setString("nickName", nickName);
		return (User)query.uniqueResult();
	}



		/* (non-Javadoc)
		 * @see com.EstateManager.Dao.IModelBasicOperate#update(java.lang.Object)
		 */
		@Override
		public void update(User entity) throws HibernateException {
			Session session = this.getCurrentSession();
			session.update(entity);
		}



		/* (non-Javadoc)
		 * @see com.EstateManager.Dao.IModelBasicOperate#merge(java.lang.Object)
		 */
		@Override
		public void merge(User entity) throws HibernateException {
			this.getCurrentSession().merge(entity);
		}


}
